
exports.GAMutableArray = Core.Class.subclass({

    classname: 'GAMutableArray',

	initialize: function(array)
	{
		if(array)
		{
		this._array = JSON.parse(JSON.stringify(array));
		this._length = array.length;
		}
		else
		{
			this._array = new Array();
			this._length = 0;
		}
	},
	
	sortFunctionAsc: function(a,b)
	{
		return (a-b);	
	},
	
	sortFunctionDesc: function(a,b)
	{
			return (b-a);	
	},
	
	numericSort: function(desc)
	{
		if(desc && desc == true)
		{
			this._array.sort(this.sortFunctionDesc());
			return;
		}
		this._array.sort(this.sortFunctionAsc());
	},
	
	addObject:function(object){
		if(object != null){
			this._array.push(object);
			this._length++;
		}
	},
	
	objectAtIndex: function(index)
	{
		if (this.getLength() >= 0 && index >= 0)
		{
		        return this._array[+index];
		}
		return null;
	},

	getLength: function()
	{
		return this._length;
	},
	
	getArray:function(){       ////////
		if(this.checkIfReleased()){
			return this._array;
		}
	},
	
	pop:function()
	{
		if(this.checkIfReleased())
		{
			this._length--;
		  	return this._array.pop(); 
		}
	},
	
	removeLastObject:function()
	{
		if(this.checkIfReleased()){
		  		this._array.pop();
				this._length--; 
		}  
	},
	insertObjectAtIndex:function(index,object)
	{
		 
		if(index != null && object != null) {
			this._array.splice(index,0,object);
			this._length++;
		}
		  
	},
	removeObjectAtIndex:function(index){
		if(this.checkIfReleased()){
				if(index != null && index<this._length) {
				this._array.splice(index,1);
				this._length--;
			}
			else
			{
		     	console.log("GA MUTABLE ARRAY Index not Exist");		
			}
		}
		
	},
	removeObjectsInRange: function(index,range)
	{
		if(index != null) 
		{
			this._array.splice(index,range); // if range == null it will remove all objects after starting index till the end
			this._length = this._array.length;
		}
		
	},
	
	removeObject: function(object)
	{
		if(this.checkIfReleased() && object)
		{
			for(var i=0 ; i<this.getLength() ; )
			{
				var tempObject = this.objectAtIndex(i);
				if(tempObject && tempObject == object)
				{
					this.removeObjectAtIndex(i);
				}
				else
				{
					i++;
				}
			}
		}
	},
	
	replaceObjectAtindex:function(index,object){
		if(this.checkIfReleased())
		{
			if(index != null && object != null && index<this._length) {
				this._array.splice(index,1,object);
			}
			else
			{
			 	 console.log("GA MUTABLE ARRAY Index not Exists.");
			}	
			}
	},
	
	checkIfReleased: function()
	{
		if(this._length == 0)
		{
			console.log("GAMutableArray is Empty.");
			return false;
		}
		else
		{
			return true;
		}
	},
	
	addArray:function (arrayObject)
	{
		var i =0;
											 
		for(i; i < arrayObject._length; i++)
		{
			this.addObject(arrayObject.objectAtIndex(i));
		}
		
	},

	destroyWithObjects: function() 
	{
		for(var i=0 ; i<this.getLength() ; i++) 
		{
			var object = this.objectAtIndex(i);
			if(object)
			{
				object.destroy();
				object = null;
			}
		}
		this.destroy();
	},

	destroy:function() {
		delete this._array;
		this._array = null;
	}
});